package controller;

import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/reg")
public class RegServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setCharacterEncoding("utf8");
        //1.获取到请求中的参数
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        //进行校验
        if (username==null || "".equals(username)||password==null||"".equals(password)){
            //请求内容缺失，登录失败~
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前的用户名或密码为空！");
            return;
        }
        //2.根据提交的用户名和密码进行注册账号
        UserDao userDao=new UserDao();
        boolean b = userDao.regUser(username, password);
        if (b){
            // 注册成功
            //4.返回一个重定向报文，跳转到博客列表页
            resp.sendRedirect("blog_login.jsp");//302
        }else {
            // 注册失败
            resp.sendRedirect("reg.jsp");
        }
    }
}
